home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / NextAnswers / 1435_methods_returning_floats.rtf < prev    next >
Text File  |  1993-11-08  |  1KB  |  38 lines

  1. {\rtf0\ansi{\fonttbl\f0\fnil Times-Roman;\f1\fmodern Courier;\f3\fmodern Ohlfs;}
  2. \paperw10620
  3. \paperh7200
  4. \margl120
  5. \margr120
  6. {\colortbl;\red0\green0\blue0;}
  7. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\f0\b0\i0\ulnone\fs28\fc0\cf0 Q:  I am trying to perform a method that returns a float.  In the Concepts manual, page 3-20, it indicates I can just cast the return value to float like this:\
  8. \
  9.  
  10. \f1\fs24     float    myGray;\
  11.     myGray = (float)[textField perform:@selector(backgroundGray)];\
  12.     \
  13.  
  14. \f0\fs28 But I get a compile-time error of "pointer value used where a float was expected."  What can I do?\
  15. \
  16. A:   The 
  17. \b perform
  18. \b0  method is prototyped to return a value of type 
  19. \b id
  20. \b0 .  While the C compiler lets you cast of lot of things, it says enough is enough when you try cast something of type 
  21. \b id
  22. \b0  to be 
  23. \b float
  24. \b0 .  Here is one workaround which allows you to get the value you want:\
  25. \
  26.  
  27. \pard\tx1140\tx2300\tx3440\tx4600\tx5760\tx6900\tx8060\tx9200\tx10360\tx11520\f1\fs24\fc1\cf1     id myId;\
  28.     float myGray;\
  29.     myId = [textField perform:@selector(backgroundGray)];\
  30.     myFloat = *(float *)&myId;\
  31.  
  32. \pard\tx960\tx1920\tx2880\tx3840\tx4800\tx5760\tx6720\tx7680\tx8640\tx9600\f0\fs28\fc0\cf0 \
  33. Valid for 1.0, 2.0, 3.0\
  34. \
  35. QA557\
  36. \
  37.  
  38.